﻿using FlyingEye.Monitors.Models;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace FlyingEye.EntityFrameworkCore
{
    public class MonitorServiceDbContext : AbpDbContext<MonitorServiceDbContext>
    {
        public MonitorServiceDbContext(DbContextOptions<MonitorServiceDbContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DeviceClientModel>(builder =>
            {
                builder.ToTable(
                MonitorServiceDbConsts.DbTablePrefix + "DeviceClients",
                MonitorServiceDbConsts.DbSchema);

                // 设备资源号是唯一的
                builder.HasIndex(e => e.ResourceId)
                .IsUnique();

                // 基地和工序名称的组合是唯一的
                builder.HasIndex(e => new { e.Site, e.ProcessName })
                .IsUnique();

                builder.HasMany<ReadNumericValueTaskModel>()
                .WithOne()
                .HasForeignKey(e => e.DeviceClientId)
                .IsRequired();

                builder.HasMany<ReadStringValueTaskModel>()
                .WithOne()
                .HasForeignKey(e => e.DeviceClientId)
                .IsRequired();
            });

            modelBuilder.Entity<ReadNumericValueTaskModel>(builder =>
            {
                builder.ToTable(
                MonitorServiceDbConsts.DbTablePrefix + "ReadNumericValueTasks",
                MonitorServiceDbConsts.DbSchema);

                builder.HasMany<NumericValueMonitorClientModel>()
                .WithOne()
                .HasForeignKey(e => e.ReadNumericValueTaskId)
                .IsRequired();

                builder.HasMany<NumericValueMonitorGroupModel>()
                .WithOne()
                .HasForeignKey(e => e.ReadNumericValueTaskId)
                .IsRequired();

                builder.HasIndex(e => e.UniqueHashKey)
                .IsUnique();

                builder.HasIndex(e => e.TaskName)
                .IsUnique();
            });

            modelBuilder.Entity<ReadStringValueTaskModel>(builder =>
            {
                builder.ToTable(
                 MonitorServiceDbConsts.DbTablePrefix + "ReadStringValueTasks",
                 MonitorServiceDbConsts.DbSchema);

                builder.HasMany<StringValueMonitorClientModel>()
                .WithOne()
                .HasForeignKey(e => e.ReadStringValueTaskId)
                .IsRequired();

                builder.HasMany<StringValueMonitorGroupModel>()
                .WithOne()
                .HasForeignKey(e => e.ReadStringValueTaskId)
                .IsRequired();

                builder.HasIndex(e => e.UniqueHashKey)
                .IsUnique();

                builder.HasIndex(e => e.TaskName)
                .IsUnique();
            });

            modelBuilder.Entity<StringValueMonitorClientModel>(builder =>
            {
                builder.ToTable(
                 MonitorServiceDbConsts.DbTablePrefix + "StringValueMonitorClients",
                 MonitorServiceDbConsts.DbSchema);

                // 客户端名称和监听任务 ID 的组合是唯一的
                builder.HasIndex(e => new { e.ReadStringValueTaskId, e.ClientName })
                .IsUnique();
            });

            modelBuilder.Entity<StringValueMonitorGroupModel>(builder =>
            {
                builder.ToTable(
                 MonitorServiceDbConsts.DbTablePrefix + "StringValueMonitorGroups",
                 MonitorServiceDbConsts.DbSchema);

                // 组名称和监听任务 ID 的组合是唯一的
                builder.HasIndex(e => new { e.ReadStringValueTaskId, e.GroupName })
                .IsUnique();
            });

            modelBuilder.Entity<NumericValueMonitorClientModel>(builder =>
            {
                builder.ToTable(
                 MonitorServiceDbConsts.DbTablePrefix + "NumericValueMonitorClients",
                 MonitorServiceDbConsts.DbSchema);

                // 客户端名称和监听任务 ID 的组合是唯一的
                builder.HasIndex(e => new { e.ReadNumericValueTaskId, e.ClientName })
                .IsUnique();
            });

            modelBuilder.Entity<NumericValueMonitorGroupModel>(builder =>
            {
                builder.ToTable(
                 MonitorServiceDbConsts.DbTablePrefix + "NumericValueMonitorGroups",
                 MonitorServiceDbConsts.DbSchema);

                // 组名称和监听任务 ID 的组合是唯一的
                builder.HasIndex(e => new { e.ReadNumericValueTaskId, e.GroupName })
                .IsUnique();
            });

            base.OnModelCreating(modelBuilder);
        }
    }
}
